import { createStore } from 'vuex'
import router from "@/router";

export default createStore({
    state: {
        token: "",
        userInfo: {},
        menuList: {},
        hasRoutes: false,
        editableTabsValue: '/index',
        editableTabs: [
            {
                title: '首页',
                name: '/index'
            }
        ],
        sideType: true,
    },
    getters: {
        GET_TOKEN: state => {
            return state.token || sessionStorage.getItem("token");
        },
        GET_USERINFO: state => {
            return state.userInfo || JSON.parse(sessionStorage.getItem("userInfo"));
        },
        GET_TYPE: state => state.sideType
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
            sessionStorage.setItem("token", token);
        },
        SET_MENULIST: (state, menuList) => {
            const menu = JSON.stringify(menuList);
            state.menuList = JSON.parse(menu)
            sessionStorage.setItem("menuList", menu);
        },
        SET_USERINFO: (state, userInfo) => {
            const user = JSON.stringify(userInfo);
            state.userInfo = JSON.parse(user)
            sessionStorage.setItem("userInfo", user);
        },
        SET_ROUTES_STATE: (state, hasRoutes) => {
            const routes = JSON.stringify(hasRoutes);
            state.hasRoutes = JSON.parse(routes)
            sessionStorage.setItem("hasRoutes", routes);
        },
        ADD_TABS: (state, tab) => {
            if (state.editableTabs.findIndex(e => e.name === tab.path) === -1) {
                state.editableTabs.push({
                    title: tab.name,
                    name: tab.path
                })
            }
            state.editableTabsValue = tab.path
        },
        RESET_TABS: (state) => {
            state.editableTabsValue = '/index';
            state.editableTabs = [
                {
                    title: '首页',
                    name: '/index'
                }
            ]
        },
        changeSideType(state) {
            state.sideType = !state.sideType
        }
    },
    actions: {
        // 安全退出
        logout() {
            window.sessionStorage.clear();
            router.replace("/login")
        }
    },
    modules: {}
})
